package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * C端地址簿相关接口
 */
@Api(value = "/user/addressBook", tags = {"C端地址簿相关接口"})
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 保存售货地址功能
     * @param addressBook
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "AddressBook", name = "addressBook", value = "", required = true)
    })
    @ApiOperation(value = "保存售货地址功能", notes = "保存售货地址功能", httpMethod = "POST")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook) {
        return addressBookService.save(addressBook);
    }

    /**
     * 查询该用户所有地址信息
     * @return
     */
    @ApiOperation(value = "查询该用户所有地址信息", notes = "查询该用户所有地址信息", httpMethod = "GET")
    @GetMapping("/list")
    public Result<List<AddressBook>> list() {
        return addressBookService.list();
    }

    /**
     * 修改该用户默认收货地址
     * @return
     */
    @ApiOperation(value = "修改该用户默认收货地址", notes = "查询该用户默认收货地址", httpMethod = "GET")
    @PutMapping("/default")
    public Result defaultAddress(@RequestBody AddressBook addressBook) {
        return addressBookService.updateDefaultAddress(addressBook);
    }

    /**
     * 修改回显收货地址
     * @param userId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "userId", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "AddressId", value = "", required = true)
    })
    @ApiOperation(value = "修改回显收货地址", notes = "修改回显收货地址", httpMethod = "GET")
    @GetMapping("/{userId}")
    public Result<AddressBook> getAddress(@PathVariable("userId") Long userId,@RequestParam("id") Long AddressId) {
        return addressBookService.getAddress(userId,AddressId);
    }

    /**
     * 删除该用户收货地址
     * @param AddressId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "AddressId", value = "", required = true)
    })
    @ApiOperation(value = "删除该用户收货地址", notes = "删除该用户收货地址", httpMethod = "DELETE")
    @DeleteMapping
    public Result delete(@RequestParam("id")Long AddressId) {
        return addressBookService.delete(AddressId);
    }

    /**
     * 更新用户收货地址信息
     * @param addressBook
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "AddressBook", name = "addressBook", value = "", required = true)
    })
    @ApiOperation(value = "更新用户收货地址信息", notes = "更新用户收货地址信息", httpMethod = "PUT")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        return addressBookService.update(addressBook);
    }
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        return addressBookService.defaultAddress();
    }
}
